package com.qyhl.webtv.module_microvideo.shortvideo.theme.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoBean;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoThemeBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_microvideo.R;
import com.qyhl.webtv.module_microvideo.R2;
import com.qyhl.webtv.module_microvideo.shortvideo.theme.detail.ShortVideoThemeDetailContract;
import com.qyhl.webtv.module_microvideo.shortvideo.theme.detail.ShortVideoThemeDetailPresenter;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShortVideoThemeNewFragment extends BaseFragment implements ShortVideoThemeDetailContract.ShortVideoThemeDetailView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    private String tagId;

    private boolean isAll = false;

    public void setTagId(String tagId) {
        this.tagId = tagId;
    }

    public void setActivityId(String activityId) {
        this.activityId = activityId;
    }

    public void setAll(boolean all) {
        isAll = all;
    }

    private String activityId;

    private CommonAdapter<ShortVideoBean> commonAdapter;

    private List<ShortVideoBean> mDataList = new ArrayList<>();

    private ShortVideoThemeDetailPresenter mPresenter;

    private int page = 1;

    public static ShortVideoThemeNewFragment newInstance(String tagId,String activityId,boolean isAll) {
        ShortVideoThemeNewFragment fragment = new ShortVideoThemeNewFragment();
        fragment.setAll(isAll);
        fragment.setTagId(tagId);
        fragment.setActivityId(activityId);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.micro_fragment_short_video_theme, null);
    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new ShortVideoThemeDetailPresenter(this);


        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);

        recycleView.setLayoutManager(new GridLayoutManager(getContext(), 2));

        recycleView.setAdapter(commonAdapter = new CommonAdapter<ShortVideoBean>(getContext(), R.layout.micro_item_short_video_theme_new, mDataList) {
            @Override
            protected void convert(ViewHolder holder, ShortVideoBean shortVideoBean, int position) {
                holder.setText(R.id.title, shortVideoBean.getTitle());

                holder.setText(R.id.description, shortVideoBean.getInfo());

                holder.setText(R.id.nickName, shortVideoBean.getNickname());

                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(ShortVideoThemeNewFragment.this)
                        .load(shortVideoBean.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_video_default).error(R.drawable.cover_video_default))
                        .into(cover);

                RoundedImageView header = holder.getView(R.id.head_cover);
                Glide.with(ShortVideoThemeNewFragment.this)
                        .load(shortVideoBean.getUserLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default))
                        .into(header);
            }
        });

        mPresenter.getVideoList(tagId, page + "",isAll);

    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            page = 1;
            mPresenter.getVideoList(tagId, page + "",isAll);
        });

        refresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getVideoList(tagId, page + "",isAll);
        });

        refresh.setOnLoadMoreListener(refreshLayout -> mPresenter.getVideoList(tagId, page + "",isAll));

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", (Serializable) mDataList);
                bundle.putInt("position", position);
                bundle.putInt("page", page);
                if(isAll){
                    bundle.putInt("type", 8);
                    bundle.putString("actId", activityId);
                }else{
                    bundle.putInt("type", 1);
                    bundle.putString("actId", tagId);
                }

                RouterManager.router(ARouterPathConstant.SHORT_VIDEO_PLAY_QX,bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void setVideoList(List<ShortVideoBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        page++;
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setListError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(getContext())) {
                showToast("网络异常，请检查您的网络！");
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            if (!NetUtil.isNetworkAvalible(getContext())) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }

    @Override
    public void setDetail(ShortVideoThemeBean bean) {

    }

    @Override
    public void setDetailError(String msg) {

    }

    @Override
    public void initImmersionBar() {

    }
}
